package cn.chainco.cts.jdo;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 * 地名表
 */
@Entity
@Table(name = "m68_point")
public class Point implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final int PLACE_TYPE_SEAPORT = 0; // 海港
	public static final int PLACE_TYPE_AIRPORT = 1; // 空港
	public static final int PLACE_TYPE_INLAND = 2; // 内陆点

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;

	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
	@JoinColumn(name = "countryId")
	private Country country; // 国家名或州名

	@Column(nullable = false)
	private int type; // 地名类型

	@Column(nullable = false, length = 60)
	private String name; // 地点名称

	@Column
	private Integer basePort; // 基本港：空或0非基本港；1基本港

	@Column
	private Double longitude; // 经度

	@Column
	private Double latitude; // 纬度

	@Column
	private Integer zoomLevel; // 缩放级别

	@Column(length = 11)
	private String zipCode; // 邮政编码

	@Column(length = 3)
	private String airCode = ""; // 机场代码

	@Column(nullable = false, length = 100)
	private String fullname; // 全称，包含国家

	@Column(nullable = false, length = 100)
	private String fullnameNoSymbol; // 搜索用全称

	@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY)
	@JoinColumn(name = "pointGroupId")
	private PointGroup pointGroup; // 地名分组

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Country getCountry() {
		return country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getName() {
		if (this.type == Point.PLACE_TYPE_SEAPORT
				|| this.type == Point.PLACE_TYPE_AIRPORT) {
			return name.toUpperCase();
		} else {
			return name;
		}
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAirCode() {
		return airCode;
	}

	public void setAirCode(String airCode) {
		this.airCode = airCode;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public String getFullname() {
		if (this.type == Point.PLACE_TYPE_SEAPORT
				|| this.type == Point.PLACE_TYPE_AIRPORT) {
			return fullname.toUpperCase();
		} else {
			return fullname;
		}
	}

	public void setFullname(String fullname) {
		this.fullname = fullname;
	}

	public String getFullnameNoSymbol() {
		return fullnameNoSymbol;
	}

	public void setFullnameNoSymbol(String fullnameNoSymbol) {
		this.fullnameNoSymbol = fullnameNoSymbol;
	}

	public String fullname() {
		if (this.airCode == null || this.airCode.length() == 0) {
			return name + ", " + country.getCountryName();
		} else {
			return name + "(" + airCode + "), " + country.getCountryName();
		}
	}

	public String fullnameNoSymbol() {
		String s = this.fullname;
		s = s.replaceAll(",", "").replaceAll(" ", "").replaceAll("[.]", "")
				.replaceAll("-", "").replaceAll("'", "").replaceAll("[(]", "")
				.replaceAll("[)]", "");
		return s.toUpperCase();
	}

	public String fullnameNoSymbolOnlyAirPort() {
		String s = name + country.getCountryName();
		s = s.replaceAll(",", "").replaceAll(" ", "").replaceAll("[.]", "")
				.replaceAll("-", "").replaceAll("'", "");
		return s.toUpperCase();
	}

	public PointGroup getPointGroup() {
		return pointGroup;
	}

	public void setPointGroup(PointGroup pointGroup) {
		this.pointGroup = pointGroup;
	}

	public Integer getBasePort() {
		return basePort;
	}

	public void setBasePort(Integer basePort) {
		this.basePort = basePort;
	}

	public Double getLongitude() {
		return longitude;
	}

	public void setLongitude(Double longitude) {
		this.longitude = longitude;
	}

	public Double getLatitude() {
		return latitude;
	}

	public void setLatitude(Double latitude) {
		this.latitude = latitude;
	}

	public Integer getZoomLevel() {
		return zoomLevel;
	}

	public void setZoomLevel(Integer zoomLevel) {
		this.zoomLevel = zoomLevel;
	}

}
